#include "Mechanical.h"
#include "RSManager.h"
#include "Object.h"
#include "Box.h"
#include "Sprite.h"
#include "Arm.h"
#include "Boss3Fire.h"
#include "ShareComponents.h"
#include "MainGame.h"
#include "EnemyDie.h"
#include "BossHand.h"
#define MIN_A   2.3f * 3.14f / 4.0f 
#define MAX_A   3 * 3.14f / 2.0f

#define MAX_A_R   1.7f * 3.14f / 4.0f 
#define MIN_A_R   -3.14f / 2.0f
#define SHOOT_DOWN -3.14f / 2.0f
Mechanical::Mechanical(Box* box) : Object(box,Obj_STATIC) 
{
	init();
}

Mechanical::~Mechanical(void)
{
}

void Mechanical::init()
{
	_hits = 20;
	_sprite = RSManager::getInstance()->getSprite(BOSS3);
	delay  = 0;
	float delta = 5 * 3.14f / 4.0f;
	float dx = 4;
	float omage = 0.04f;
	Arm* arm1, *arm2, * arm3,*arm4;
	int X, Y;
	X = _box->_position.x + 135;
	Y = _box->_position.y + 224;
	_left = new Arm(D3DXVECTOR2(X,Y), 0,3.14f ,omage,MIN_A,MAX_A);	
	arm1 = new Arm(D3DXVECTOR2(X,Y), 32 * 1 - dx,3.14f,omage,MIN_A ,MAX_A);	
	arm2 = new Arm(D3DXVECTOR2(X,Y), 32 * 2 - dx,3.14f,omage,MIN_A ,MAX_A);	
	arm3 = new Arm(D3DXVECTOR2(X,Y), 32 * 3 - dx,3.14f,omage,MIN_A ,MAX_A);	
	arm4 = new Arm(D3DXVECTOR2(X,Y), 32 * 4 - dx,3.14f,omage,MIN_A ,MAX_A);	
	
	_left->addNext(arm1);
	arm1->addNext(arm2);
	arm2->addNext(arm3);
	arm3->addNext(arm4);
	left_hand = new BossHand(new Box(D3DXVECTOR2(X,Y),D3DXVECTOR2(),D3DXVECTOR2()));
	left_hand->_parent = arm4->getBox();
	X += 200;
	_right = new  Arm(D3DXVECTOR2(X,Y), 0,0 ,omage,MIN_A_R,MAX_A_R);
	_right->init();
	arm1 = new  Arm(D3DXVECTOR2(X,Y), 32* 1 - dx,0 ,omage,MIN_A_R,MAX_A_R);	
	arm2 = new  Arm(D3DXVECTOR2(X,Y), 32* 2 - dx,0 ,omage,MIN_A_R,MAX_A_R);
	arm3 = new  Arm(D3DXVECTOR2(X,Y), 32* 3 - dx,0 ,omage,MIN_A_R,MAX_A_R);
	arm4 = new  Arm(D3DXVECTOR2(X,Y), 32* 4 - dx,0 ,omage,MIN_A_R,MAX_A_R);		
	_right->addNext(arm1);
	arm1->addNext(arm2);
	arm2->addNext(arm3);
	arm3->addNext(arm4);
	_left->_k = 1.0f;
	arm1->_k = -1.0f;
	arm2->_k = -1.0f;
	arm3->_k = -1.0f;
	arm4->_k = -1.0f;
	
	_right->_k = -1.0f;
	_changeStateTime = 0;
	_shootTime = 0;
	_state = WAVE;
	
	_left->_isParent = true;
	_right->_isParent = true;

	right_hand = new BossHand(new Box(D3DXVECTOR2(X,Y),D3DXVECTOR2(),D3DXVECTOR2()));
	right_hand->_parent = arm4->getBox();
	create_arm = false;
	left_exp = false;
	right_exp = false;
}

void Mechanical::update()
{
	if(_hits < 0)
	{
		_isAlive = false;
		gl_MainGame->removeWorldObject(left_hand);
		gl_MainGame->removeWorldObject(right_hand);
		EnemyDie* x = new EnemyDie(new Box(_colBox));
		x->init(EFFECT_EXPLODE1);
		gl_MainGame->addWorldObject(x);
		_colBox->_position.x += 60;
		x = new EnemyDie(new Box(_colBox));
		x->init(EFFECT_EXPLODE1);
		gl_MainGame->addWorldObject(x);		
		x = new EnemyDie(new Box(_colBox));
		x->init(EFFECT_EXPLODE1);
		gl_MainGame->addWorldObject(x);
		_colBox->_position.x -= 120;
		x = new EnemyDie(new Box(_colBox));
		x->init(EFFECT_EXPLODE1);
		gl_MainGame->addWorldObject(x);
	}
	if(!create_arm)
	{
		gl_MainGame->addWorldObject(left_hand);
		gl_MainGame->addWorldObject(right_hand);
		create_arm = true;
	}
	delay += GAME_TIME;
	if(delay > 100)
	{
		delay = 0;
		_sprite->Next();
	}		
	if(!left_hand->isAlive() && !left_exp)
	{
		Arm* i = _left;
		while(i != NULL)
		{
			EnemyDie* x = new EnemyDie(new Box(i->getBox()));
			x->init(EFFECT_EXPLODE1);
			gl_MainGame->addWorldObject(x);
			i = i->_next;
		}
		_left->_state = DEATH;
		left_exp = true;
	}

	if(!right_hand->isAlive() &&!right_exp)
	{
		right_exp = true;
		Arm* i = _right;
		while(i != NULL)
		{
			EnemyDie* x = new EnemyDie(new Box(i->getBox()));
			x->init(EFFECT_EXPLODE1);
			gl_MainGame->addWorldObject(x);
			i = i->_next;
		}
		_right->_state = DEATH;
	}

	_shootTime += GAME_TIME;
	_changeStateTime += GAME_TIME;
	if(_changeStateTime > 5000)
	{
		_changeStateTime = 0;
		_state  = ArmState((_state + 1) % 3);
		switch (_state)
		{
		case WAVE:
			{
				if(_left ->_state != DEATH)
				{
					_left->_state = _state;
					_left->angle = MAX_A;
				}
				if(_right ->_state != DEATH)
				{
					_right->_state = _state;
					_right->angle = MIN_A_R;
				}
			}
			break;
		case ROTATE:
			{
				if(_left ->_state != DEATH)
				{
					_left->_state = _state;
					//_left->angle = MAX_A;
					_left->setAngle(MAX_A);
					_left->_k = 1.0f;
				}
				if(_right ->_state != DEATH)
				{
					_right->_state = _state;
					//_right->angle = MIN_A_R;
					_right->setAngle(MIN_A_R);
					_right->_k = -1.0f;
				}
			}
			break;
		case FOLLOW:
			{
				if(_left ->_state != DEATH)
				{
					_left->_state = _state;
					_left->_next->setAngle(_left->angle);
				}
				if(_right ->_state != DEATH)
				{
					_right->_state = _state;
					_right->_next->setAngle(_right->angle);
				}
			}
			break;
		}
	}
	if(_shootTime > 5000)
	{
		_shootTime = 0;
		//shoot here
		_shoot(SHOOT_DOWN);
		_shoot(SHOOT_DOWN + 3.14f /6.0f);
		_shoot(SHOOT_DOWN - 3.14f /6.0f);
	}
	if(_left ->_state != DEATH)
		_left->update();
	if(_right ->_state != DEATH)
		_right->update();
}

void Mechanical::render()
{
	_sprite->Render(_box->_position);
	if(_left ->_state != DEATH)
		_left->render();
	if(_right ->_state != DEATH)
		_right->render();
}

Box* Mechanical::getCollisionBox()
{
	_colBox->_position = _box->_position;
	_colBox->_size.x = _colBox->_size.y = 100;
	_colBox->_position.x += 200;
	_colBox->_position.y += 200;
	return _colBox;
}

void Mechanical::_shoot(float _angle)
{
	int X, Y;
	X = _box->_position.x + 240;
	Y = _box->_position.y + 260;
	Boss3Fire* bf = new Boss3Fire(new Box(D3DXVECTOR2(X,Y),D3DXVECTOR2(),D3DXVECTOR2()),_angle);
	gl_MainGame->addWorldObject(bf);
}
